const express = require("express");
const app = express();
const getBaseData = require("./getData/getBase");
const getLastBaseData = require("./getData/getLastBaseData");
const getUser = require("./getData/getUser");
const getActive = require("./getData/getActive");
const getVisit = require("./getData/getVisit");
const getStartup = require("./getData/getStartup");
const getStop = require("./getData/getStop");
const getAvgStop = require("./getData/getAvgStop");
const getTotal = require("./getData/getTotal");
const getLogin = require("./getData/getLogin");
const getCode = require("./getData/getCode");
const userInfo = {
  email:"391733634@qq.com", // 账号
  password:"xuewen12a0", // 密码
  id_app:"153678" // app的统计id
}
app.use("/code", express.static(__dirname + "/code"));

async function getData(start, end) {
  let { data: baseData } = await getBaseData(userInfo.id_app,start, end);
  let { data: lastBaseData } = await getLastBaseData(userInfo.id_app ,start, end);
  let { data: userData } = await getUser(userInfo.id_app,start, end);
  let { data: activeData } = await getActive(userInfo.id_app,start, end);
  let { data: visitData } = await getVisit(userInfo.id_app,start, end);
  let { data: startupData } = await getStartup(userInfo.id_app,start, end);
  let { data: stopData } = await getStop(userInfo.id_app,start, end);
  let { data: avgStopData } = await getAvgStop(userInfo.id_app,start, end);
  let { data: totalData } = await getTotal(userInfo.id_app,start, end);
  return {
    baseData,
    lastBaseData,
    userData,
    activeData,
    visitData,
    startupData,
    stopData,
    avgStopData,
    totalData,
  };
}
app.get("/code", async (req, res) => {
  let data = await getCode();
  res.send({ data });
});

app.get("/login", async (req, res) => {
  try {
    let { code, id } = req.query;
    let data = await getLogin(userInfo,code, id);
    res.send({ data });
  } catch (e) {
    console.log(e);
  }
});

app.get("/getdata", async (req, res) => {
  let { start, end } = req.query;
  let data = await getData(start, end);
  res.send({ data });
});

app.listen(3001, () => {
  console.log("服务器已启动");
});
